<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class pmember extends CI_Controller {
	
	public function index()
	{
		$all_session = $this->session->all_userdata();     	

    	if(@$all_session['login'][0]['mem_type'] != 2 && @$all_session['login'][0]['mem_type'] != 1)
		{
		  	redirect('authen');
  		}

		$this->load->library('pagination');
		$config['first_tag_open'] = $config['last_tag_open']= $config['next_tag_open']= $config['prev_tag_open'] = $config['num_tag_open'] = '<li>';
        $config['first_tag_close'] = $config['last_tag_close']= $config['next_tag_close']= $config['prev_tag_close'] = $config['num_tag_close'] = '</li>';
        $config['cur_tag_open'] = "<li><span><b>";
        $config['cur_tag_close'] = "</b></span></li>";

		$config['base_url'] = base_url().'index.php/pmember/index';
		$config['total_rows'] = count($this->db->get('member')->result_array());
		$config['per_page'] = 50;
				 
		$per_page = $config['per_page'];
		$uri = $this->uri->segment(3);




		$this->pagination->initialize($config); 


		$data['member'] = $this->pmember_model->index($per_page ,$uri);

		$data['total_rows'] = count($data['member']);
	
		$data['admin'] = $this->pmember_model->sum_status(2);
		$data['mod'] = $this->pmember_model->sum_status(1);
		$data['mem'] = $this->pmember_model->sum_status(0);
		$data['ban'] = $this->pmember_model->sum_status(3);


		$data['edit'] = $this->pmember_model->index($per_page ,$uri);
		$data['delete'] = $this->pmember_model->index($per_page ,$uri);
		$this->load->view('pmember',$data);

	}

	public function search()
	{
		$all_session = $this->session->all_userdata();     	

    	if(@$all_session['login'][0]['mem_type'] != 2 && @$all_session['login'][0]['mem_type'] != 1)
		{
		  	redirect('authen');
  		}

		$all_session = $this->session->all_userdata();
		//////////////////////////////////////////////////////
		//////////////////// pagination //////////////////////
		//////////////////////////////////////////////////////

		$this->load->library('pagination');
		$config['first_tag_open'] = $config['last_tag_open']= $config['next_tag_open']= $config['prev_tag_open'] = $config['num_tag_open'] = '<li>';
        $config['first_tag_close'] = $config['last_tag_close']= $config['next_tag_close']= $config['prev_tag_close'] = $config['num_tag_close'] = '</li>';
        $config['cur_tag_open'] = "<li><span><b>";
        $config['cur_tag_close'] = "</b></span></li>";

		
		$config['per_page'] = 50;
				 
		$per_page = $config['per_page'];
		

		//////////////////////////////////////////////////////
		/////////////////////// search ///////////////////////
		//////////////////////////////////////////////////////

		if($this->input->post('search') != null) {

			$session['search'] = $this->input->post('search');
			$this->session->set_userdata($session);
		
			$config['base_url'] = base_url().'index.php/pmember/search/'; //.$this->uri->segment(3);
			$uri = $this->uri->segment(3);

			$input = array(
				'search' => $this->input->post('search')//$this->uri->segment(3)
			);

			$input['search'] = urldecode($input['search']);
			$config['total_rows'] = count($this->pmember_model->count_search($input));
			$data['member'] = $this->pmember_model->search($input ,$per_page ,$uri);
			
		
		} else if ($this->input->post('search') == null) {

			$config['base_url'] = base_url().'index.php/pmember/search/'; //.$this->uri->segment(3);
			$uri = $this->uri->segment(3);

			$input = array(
				'search' => $all_session['search']
			);

			$input['search'] = urldecode($input['search']);
			$config['total_rows'] = count($this->pmember_model->count_search($input));
			$data['member'] = $this->pmember_model->search($input ,$per_page ,$uri);


		}

		//////////////////////////////////////////////////////
		/////////////////////// data /////////////////////////
		//////////////////////////////////////////////////////
		$data['all'] = $this->pmember_model->count_search($input);

		$data['admin'] = 0;
		$data['mod'] = 0;
		$data['mem'] = 0;
		$data['ban'] = 0;


		for($i=0;$i<count($data['all']);$i++)
		{
			if($data['all'][$i]['mem_type'] == 0)
			{
				$data['mem']+=1;
			} else if ($data['all'][$i]['mem_type'] == 1) {
				$data['mod']+=1;
			} else if ($data['all'][$i]['mem_type'] == 2) {
				$data['admin']+=1;
			} else if ($data['all'][$i]['mem_type'] == 3) {
				$data['ban']+=1;
			}
		}

		
		$data['all'] = $data['mem'] + $data['mod'] + $data['admin'] + $data['ban'];
		
			
		$this->pagination->initialize($config); 

		$data['edit'] = $this->pmember_model->index($per_page ,$uri);
		$data['delete'] = $this->pmember_model->index($per_page ,$uri);

		$data['total_rows'] = count($data['member']);
		
		$this->load->view('pmember',$data);
	}


	public function register()
	{
		$all_session = $this->session->all_userdata();     	

    	if(@$all_session['login'][0]['mem_type'] != 2 && @$all_session['login'][0]['mem_type'] != 1)
		{
		  	redirect('authen');
  		}

		$this->load->view('pmember_register');
	}

	public function add_register()
	{
		////////////////////////////////////////////
		////// find date for mem_dateregister //////
		////////////////////////////////////////////
		$datestring = "%Y-%m-%d";
		$time = time();
		
		////////////////////////////////////////////
		//////////////// upload file ///////////////
		////////////////////////////////////////////
		$config['upload_path'] = './uploads/';
		$config['allowed_types'] = 'gif|jpg|png';
		$config['max_size']	= '1000';
		$config['max_width']  = '2000';
		$config['max_height']  = '2000';
		$id = uniqid();
		$config['file_name'] = $id;
		

		$this->load->library('upload', $config);


		if ( ! $this->upload->do_upload())
		{	
			$error = array('error' => $this->upload->display_errors());
			//$this->load->view('upload_form', $error);
		}
		else
		{
			$data = array('upload_data' => $this->upload->data());
			//$this->load->view('upload_success', $data);
		}

		
		////////////////////////////////////////////
		//////////////// input field ///////////////
		////////////////////////////////////////////
	
		$input = array(
			'mem_name' => $this->input->post('mem_name'),
			'mem_mail' => $this->input->post('mem_mail'),
			'mem_pass' => md5($this->input->post('mem_pass')),
			'mem_type' => '0',
			'mem_fname' => $this->input->post('mem_fname'),
			'mem_lname' => $this->input->post('mem_lname'),
			'mem_tell' => $this->input->post('mem_tell'),
			'mem_address' => str_replace("\n", "<br>\n", $this->input->post('mem_address')),
			'mem_birth' => $this->input->post('mem_birth'),
			'mem_dateregister' => mdate($datestring, $time),
			'mem_image' => @$data['upload_data']['file_name']
		);

		if($input['mem_name'] != '')
		{
			$this->pmember_model->register($input);
		}	

		////////////////////////////////////////////
		///////////////// log file /////////////////
		////////////////////////////////////////////
		//$data['get_id'] = $this->pmember_model->get_member();

		$seg1 = $this->input->post('seg1');
		$seg2 = $this->input->post('seg2');
		$url = base_url().'index.php/'.$seg1.'/'.$seg2;

		$all_session = $this->session->all_userdata();		

		if($seg1 == 'pmember')
		{
			$log_file = array(						
					'log_url' => $url,
					'log_action' => 'Add New Member',
					'log_target' => $input['mem_name'],
					'log_status' => 'Pass',
					'mem_id' => $all_session['login'][0]['mem_id'],
					'log_ip' => $_SERVER['REMOTE_ADDR']						
			);
			$this->logfile_model->add_logfile($log_file);

		} 
		////////////////////////////////////////////
		////////////// check redirect //////////////
		////////////////////////////////////////////

		if($seg1 == 'pmember')
		{
			redirect('pmember');
		} else {

			$session['login'] = $this->authen_model->checklogin($input);
			$this->session->set_userdata($session);

			redirect();
		}
		
	}

	

	public function profile()
	{
		$all_session = $this->session->all_userdata();     	

    	if(@$all_session['login'][0]['mem_type'] != 2 && @$all_session['login'][0]['mem_type'] != 1)
		{
		  	redirect('authen');
  		}

	
		if($this->uri->segment(3) == null)
		{
			redirect('pmember/profile/'.$all_session['login'][0]['mem_id']);
		} 

		$uri = $this->uri->segment(3);

		$data['member'] = $this->pmember_model->profile($uri);
		$this->load->view('pmember_profile',$data);


	}

	public function update()
	{
		$all_session = $this->session->all_userdata();
	
		if($this->uri->segment(3) == null)
		{
			redirect('pmember/update/'.$all_session['login'][0]['mem_id']);
		} 

		////////////////////////////////////////////
		//////////////// input field ///////////////
		////////////////////////////////////////////

		$uri = $this->uri->segment(3);
		$data['member'] = $this->pmember_model->profile($uri);

		$this->load->view('pmember_update' ,$data);
	}

	public function update_member()
	{
		$uri = $this->uri->segment(3);
		
		////////////////////////////////////////////
		//////////////// upload file ///////////////
		////////////////////////////////////////////
		$config['upload_path'] = './uploads/';
		$config['allowed_types'] = 'gif|jpg|png';
		$config['max_size']	= '1000';
		$config['max_width']  = '2000';
		$config['max_height']  = '2000';
		$id = uniqid();
		$config['file_name'] = $id;
	
		$this->load->library('upload', $config);

		if ( ! $this->upload->do_upload())
		{	
			$error = array('error' => $this->upload->display_errors());
			if($this->input->post('mem_pass') != null){

				$input = array(
					'mem_id' => $uri,		
					'mem_type' => $this->input->post('mem_type'),		
					'mem_fname' => $this->input->post('mem_fname'),
					'mem_lname' => $this->input->post('mem_lname'),				
					'mem_address' => str_replace("\n", "<br>\n", $this->input->post('mem_address')),
					'mem_birth' => $this->input->post('mem_birth'),
					'mem_mail' => $this->input->post('mem_mail'),
					'mem_fname' => $this->input->post('mem_fname'),
					'mem_lname' => $this->input->post('mem_lname'),
					'mem_tell' => $this->input->post('mem_tell'),	
					'mem_pass' => md5($this->input->post('mem_pass')),
				);

			} else {

				$input = array(
					'mem_id' => $uri,	
					'mem_type' => $this->input->post('mem_type'),				
					'mem_fname' => $this->input->post('mem_fname'),
					'mem_lname' => $this->input->post('mem_lname'),				
					'mem_address' => str_replace("\n", "<br>\n", $this->input->post('mem_address')),
					'mem_birth' => $this->input->post('mem_birth'),
					'mem_mail' => $this->input->post('mem_mail'),
					'mem_fname' => $this->input->post('mem_fname'),
					'mem_lname' => $this->input->post('mem_lname'),
					'mem_tell' => $this->input->post('mem_tell'),		
				);

			}

		} else {
				
			$data = array('upload_data' => $this->upload->data());

			if($this->input->post('mem_pass') != null){

				$input = array(
					'mem_id' => $uri,				
					'mem_type' => $this->input->post('mem_type'),	
					'mem_fname' => $this->input->post('mem_fname'),
					'mem_lname' => $this->input->post('mem_lname'),				
					'mem_address' => str_replace("\n", "<br>\n", $this->input->post('mem_address')),
					'mem_birth' => $this->input->post('mem_birth'),
					'mem_mail' => $this->input->post('mem_mail'),
					'mem_fname' => $this->input->post('mem_fname'),
					'mem_lname' => $this->input->post('mem_lname'),
					'mem_tell' => $this->input->post('mem_tell'),					
					'mem_image' => @$data['upload_data']['file_name'],
					'mem_pass' => md5($this->input->post('mem_pass')),
				);

			} else {

				$input = array(
					'mem_id' => $uri,				
					'mem_type' => $this->input->post('mem_type'),	
					'mem_fname' => $this->input->post('mem_fname'),
					'mem_lname' => $this->input->post('mem_lname'),				
					'mem_address' => str_replace("\n", "<br>\n", $this->input->post('mem_address')),
					'mem_birth' => $this->input->post('mem_birth'),
					'mem_mail' => $this->input->post('mem_mail'),
					'mem_fname' => $this->input->post('mem_fname'),
					'mem_lname' => $this->input->post('mem_lname'),
					'mem_tell' => $this->input->post('mem_tell'),					
					'mem_image' => @$data['upload_data']['file_name']
				);
			}


		}

	
		$seg1 = $this->input->post('seg1');

		if($this->input->server('REQUEST_METHOD') && $seg1 == 'pmember')
		{			
			$old_role = $this->pmember_model->check_role($input);
			$old_role = $this->convert_role($old_role[0]['mem_type']);

			$new_role = $input['mem_type'];
			$new_role = $this->convert_role($new_role);
			$all_session = $this->session->all_userdata();

			if($old_role == $new_role)
			{
				$log_file = array(						
					'log_url' => base_url().'index.php/pmember/profile/'.$uri,
					'log_action' => 'Edit Profile',
					'log_target' => $this->input->post('mem_name'),
					'log_status' => 'Pass',
					'mem_id' => $all_session['login'][0]['mem_id'],
					'log_ip' => $_SERVER['REMOTE_ADDR']						
				);	
			} else {
				$log_file = array(						
					'log_url' => base_url().'index.php/pmember/profile/'.$uri,
					'log_action' => 'Change Role ['.$old_role.' to '.$new_role.']',
					'log_target' => $this->input->post('mem_name'),
					'log_status' => 'Pass',
					'mem_id' => $all_session['login'][0]['mem_id'],
					'log_ip' => $_SERVER['REMOTE_ADDR']						
				);		
			}

		
			$this->logfile_model->add_logfile($log_file);
			
		}	

		$this->pmember_model->update($input);
		

		if($seg1 == 'profile')
		{
			redirect('profile/index/'.$uri);
		} else if($seg1 == 'pmember') {
			redirect('pmember/profile/'.$uri);
		} else {
			redirect('pmember');
		}
	}

	public function check_user()
	{
		$input = array(
  			'check_pass' => md5($this->input->post('check_pass')),
  			'check_id' => $this->input->post('check_id')
  		);

  		$data = $this->pmember_model->check_user($input);
  		print_r($data);
	}

	public function check_already()
	{		
		$input = array(
  			'check_name' => $this->input->post('mem_name')
  		);
  		$data = $this->pmember_model->check_already($input);
  		print_r($data);
	}

	public function change_role()
	{
		$all_session = $this->session->all_userdata();     	

    	if(@$all_session['login'][0]['mem_type'] != 2 && @$all_session['login'][0]['mem_type'] != 1)
		{
		  	redirect('authen');
  		}

		$all_session = $this->session->all_userdata();	

		$mem_id = $this->input->post('mem_id');


		$input = array(
			'mem_id' => $mem_id,
			'mem_type' => $this->input->post('mem_type'),
			'mem_name' => $this->input->post('mem_name'),
			'grant_start' => $this->input->post('grant_start_'.$mem_id),
			'grant_end' => $this->input->post('grant_end_'.$mem_id)
		);		

		$old_role = $this->pmember_model->check_role($input);
		$old_role = $this->convert_role($old_role[0]['mem_type']);

		$new_role = $input['mem_type'];
		$new_role = $this->convert_role($new_role);

		$log_file = array(						
			'log_url' => base_url().'index.php/pmember/profile/'.$input['mem_id'],
			'log_action' => 'Change Role ['.$old_role.' to '.$new_role.']',
			'log_target' => $input['mem_name'],
			'log_status' => 'Pass',
			'mem_id' => $all_session['login'][0]['mem_id'],
			'log_ip' => $_SERVER['REMOTE_ADDR']						
		);	

		if($this->input->post('mem_id') != '')
		{
			$this->pmember_model->change_role($input);
			$this->logfile_model->add_logfile($log_file);
		}	
		
		redirect('pmember');
	}

	public function delete_member()
	{
		$all_session = $this->session->all_userdata();     	

    	if(@$all_session['login'][0]['mem_type'] != 2 && @$all_session['login'][0]['mem_type'] != 1)
		{
		  	redirect('authen');
  		}

		$input = array(
			'mem_id' => $this->input->post('mem_id'),
			'mem_name' => $this->input->post('mem_name')
		);
		
		$all_session = $this->session->all_userdata();

		$log_file = array(						
			'log_url' => base_url().'index.php/pmember/profile/'.$input['mem_id'],
			'log_action' => 'Delete Member',
			'log_target' => $input['mem_name'],
			'log_status' => 'Pass',
			'mem_id' => $all_session['login'][0]['mem_id'],
			'log_ip' => $_SERVER['REMOTE_ADDR']						
		);

		if($this->input->post('mem_id') != '')
		{
			$this->logfile_model->add_logfile($log_file);
			$this->pmember_model->delete_member($input);
		}
		

		redirect('pmember');
	}

	public function convert_role($new_role)
	{
		if($new_role == 0){
			$new_role = 'Member';
		} else if($new_role == 1) {
			$new_role = 'Moderator';
		} else if($new_role == 2) {
			$new_role = 'Administrator';
		} else {
			$new_role = 'Banned';
		}
		return $new_role;
	} 

}